package cn.yunyichina.provider.bone.aspect;

import cn.yunyichina.provider.framework.annotation.validate.ValidateException;
import cn.yunyichina.provider.iface.entity.common.Response;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author: admin
 * @description: aop
 */
@Aspect
@Component
public class AroundAspect extends AbstractAspect {

    private  static final Logger logger = LoggerFactory.getLogger(AroundAspect.class);
    private final int MAX_LOG_LENGTH = 1500;

    @Pointcut("execution(* cn.yunyichina.provider.bone.service.impl.*.*(String))")
    public void aroundPointCut() {
    }

    @Around("aroundPointCut()")
    public Object aroundPointCut(ProceedingJoinPoint pjp) throws Throwable {
        String classAndMethod = getClassAndMethod(pjp);
        Object returnValue;
        try {
            logger.info(classAndMethod + " 入参报文: " + pjp.getArgs()[0] + "\n");
            returnValue = pjp.proceed();
            String returnString=JSON.toJSONString(returnValue);
            int maxLength = returnString.length() >= MAX_LOG_LENGTH ? MAX_LOG_LENGTH : returnString.length();
            logger.info(classAndMethod + " 出参报文: " + returnString.substring(0, maxLength) + "\n");
        }
        catch (ValidateException e) {
            logger.error(e.getLocalizedMessage());
            return Response.failure("-1", e.getMessage());
        }
        catch (Exception e) {
            logger.error(e.getLocalizedMessage(), e);
            return Response.failure();
        }
        return returnValue;
    }
}
